package cn.jzq.demo.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.jzq.demo.entity.GeneralStudent;
import cn.jzq.demo.form.StudentForm;
import cn.jzq.demo.mapper.GeneralStudentMapper;
import cn.jzq.demo.service.IGeneralStudentService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jzq
 * @since 2018-05-22
 */
@Service
public class GeneralStudentServiceImpl extends ServiceImpl<GeneralStudentMapper, GeneralStudent> implements IGeneralStudentService {
    @Autowired
    private GeneralStudentMapper studentMapper;

    @Override
    @Cacheable(value = "student", key = "#stuNo")
    public GeneralStudent getStudentByStuNo(String stuNo) {
        return studentMapper.getStudentByStuNo(stuNo);
    }

    @Override
    @Cacheable(value = "users", key = "#stuNo")
    public GeneralStudent getTest(String stuNo) {
        return studentMapper.getStudentByStuNo(stuNo);
    }

    @Override
    public List<GeneralStudent> listStudent() {
        return studentMapper.listStudent();
    }

    @Override
    /**
     * 测试Long作为key
     */
    @Cacheable(value = "student_page", key = "#pageNum + '_' + #pageSize")
    public List<GeneralStudent> listStudentPage(Long pageNum, Long pageSize) {
        Long offset = (pageNum - 1) * pageSize;
        return studentMapper.selectPage(
                new Page<GeneralStudent>(offset.intValue(), pageSize.intValue()),
                new EntityWrapper<>());
    }

    @Override
    @CachePut(value = "student", key = "#p1.stuNo")
    public void updateStudent(Integer id, StudentForm form) {
        GeneralStudent student = studentMapper.selectById(id);
        student.setName(form.getName());
        student.setClassId(form.getClassId());
        student.setStuNo(form.getStuNo());
        student.setUpdateTime(DateUtil.date());
        student.updateById();
        studentMapper.updateById(student);
    }

    @Override
    public void insertStudent(GeneralStudent student) {
        studentMapper.insertStudent(student);
    }

    @Override
    public void saveBatch() {
        List<GeneralStudent> students = new ArrayList<>();
        for (int i = 0; i < 100000; i++) {
            Integer s = 20000 + i;
            String stuNo = s.toString();
            String name = "jzq" + i;
            GeneralStudent student = new GeneralStudent();
            student.setClassId(1);
            student.setStuNo(stuNo);
            student.setName(name);
            Date date = DateUtil.date();
            student.setCreateTime(date);
            student.setUpdateTime(date);

            students.add(student);
        }
        studentMapper.saveBatch(students);
    }
}
